# SPDX-License-Identifier: Apache-2.0

#
# SEMINIX_VERSION_CODE     0x10E0063
# SEMINIX_VERSION          1.14 or 1.14.99 (Implies -stable)
# SEMINIX_VERSION_PREFIX   1.14 or 1.14.99 (-rc1)
#
# SEMINIX_VERSION_MAJOR          1
# SEMINIX_VERSION_MINOR          14
# SEMINIX_VERSION_PATCHLEVEL     99
# SEMINIX_VERSION_EXTRAVERSION   -rc1
#
# Most outputs are converted to C macros, see `version.h.in`
#
# See also: independent and more dynamic ``BUILD_VERSION`` in
# ``git.cmake``.

include(${SEMINIX_SOURCE_DIR}/scripts/hex.cmake)
file(READ ${SEMINIX_SOURCE_DIR}/VERSION ver)

string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" _ ${ver})
set(SEMINIX_VERSION_MAJOR ${CMAKE_MATCH_1})

string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" _ ${ver})
set(SEMINIX_VERSION_MINOR ${CMAKE_MATCH_1})

string(REGEX MATCH "PATCHLEVEL = ([0-9]*)" _ ${ver})
if(NOT CMAKE_MATCH_1)
  set(SEMINIX_VERSION_PATCHLEVEL 0)
else()
  set(SEMINIX_VERSION_PATCHLEVEL ${CMAKE_MATCH_1})
endif()

string(REGEX MATCH "EXTRAVERSION = (-[a-z0-9]*)" _ ${ver})
set(SEMINIX_VERSION_EXTRAVERSION ${CMAKE_MATCH_1})

if(SEMINIX_VERSION_PATCHLEVEL)
  set(SEMINIX_VERSION ${SEMINIX_VERSION_MAJOR}.${SEMINIX_VERSION_MINOR}.${SEMINIX_VERSION_PATCHLEVEL})
else()
  set(SEMINIX_VERSION ${SEMINIX_VERSION_MAJOR}.${SEMINIX_VERSION_MINOR})
endif()
set(SEMINIX_VERSION_PREFIX ${SEMINIX_VERSION})

if(SEMINIX_VERSION_EXTRAVERSION)
  set(SEMINIX_VERSION_PREFIX ${SEMINIX_VERSION_PREFIX}${SEMINIX_VERSION_EXTRAVERSION})
endif()

if(DEFINED BUILD_VERSION)
  set(BUILD_VERSION_STR ", build: ${BUILD_VERSION}")
endif()

if(NOT NO_PRINT_VERSION)
  message(STATUS "Seminix version: ${SEMINIX_VERSION_PREFIX} (${SEMINIX_SOURCE_DIR})${BUILD_VERSION_STR}")
endif()

set(MAJOR ${SEMINIX_VERSION_MAJOR}) # Temporary convenience variable
set(MINOR ${SEMINIX_VERSION_MINOR}) # Temporary convenience variable
set(PATCH ${SEMINIX_VERSION_PATCHLEVEL}) # Temporary convenience variable

if(NOT PATCH)
  set(PATCH 0)
endif()
math(EXPR SEMINIX_VERSION_NUMBER_INT "((${MAJOR} << 24) & 0xff000000) + ((${MINOR} << 16) & 0xff0000)  + (${PATCH} & 0xffff)")

to_hex(${SEMINIX_VERSION_NUMBER_INT} SEMINIX_VERSION_CODE)
